package com.example.newbilibili;

import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.VideoView;
import androidx.appcompat.app.AppCompatActivity;
import com.danikula.videocache.HttpProxyCacheServer;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    // Declare the views and variables
    VideoView videoView;
    Button animeButton, mmdButton;
    ArrayList<String> animeList, mmdList;
    PopupWindow popupWindow;
    TextView videoName; // Declare a TextView variable

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize the views
        videoView = (VideoView) findViewById(R.id.videoView);
        animeButton = (Button) findViewById(R.id.animeButton);
        mmdButton = (Button) findViewById(R.id.mmdButton);
        videoName = (TextView) findViewById(R.id.videoName); // Initialize the TextView


        // Initialize the video lists and add some videos from the server
        animeList = new ArrayList<>();
        animeList.add ("anime/fate");
        animeList.add ("anime/缘之空1");
        animeList.add ("anime/《葬送的芙莉莲%20》第01集");
        animeList.add ("anime/《葬送的芙莉莲%20》第02集");
        animeList.add ("anime/《葬送的芙莉莲%20》第03集");
        animeList.add ("anime/《葬送的芙莉莲%20》第04集");
        animeList.add ("anime/《葬送的芙莉莲%20》第05集");
        animeList.add ("anime/《葬送的芙莉莲%20》第06集");
        animeList.add ("anime/《葬送的芙莉莲%20》第07集");
        animeList.add ("anime/《葬送的芙莉莲%20》第08集");

        mmdList = new ArrayList<>();
        mmdList.add ("mmd/上理实景");
        mmdList.add ("mmd/中国风");
        mmdList.add ("mmd/五等分的牢饭");
        mmdList.add ("mmd/换风格");
        mmdList.add ("mmd/洛天依黑白");
        mmdList.add ("mmd/病名为爱");

        // Set a click listener to the anime button
        animeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Show a pop-up window with the anime list
                showPopup(animeList);
            }
        });

        // Set a click listener to the mmd button
        mmdButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Show a pop-up window with the mmd list
                showPopup(mmdList);
            }
        });

        // Set a media controller to enable play, pause, forward, etc options.
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);
    }

    // A method to show a pop-up window with a list of videos
    private void showPopup(ArrayList<String> videoList) {
        // Inflate the pop-up window layout
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        View popupView = inflater.inflate(R.layout.popup_window, null);

        // Create the pop-up window
        popupWindow = new PopupWindow(popupView, 500, 500, true);
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);

        // Show the pop-up window at the center location
        popupWindow.showAtLocation(videoView, View.TEXT_ALIGNMENT_CENTER, 0, 0);

        // Get the reference of the list view from the pop-up layout
        ListView listView = popupView.findViewById(R.id.listView);

        // Set an adapter to the list view
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, videoList);
        listView.setAdapter(adapter);

        // Set a click listener to the list view
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Get the selected item from the list and show it in the video view
                String selectedVideo = videoList.get(position);
                Uri uri = Uri.parse("http://119.3.158.9/" + selectedVideo + ".mp4");

                // Use a proxy server to cache the videos
                HttpProxyCacheServer proxy = new HttpProxyCacheServer.Builder(MainActivity.this).build();
                String proxyUrl = proxy.getProxyUrl(uri.toString());

                // Set the proxy URL as the video source
                videoView.setVideoPath(proxyUrl);
                videoView.start();

                // Dismiss the pop-up window
                popupWindow.dismiss();

                videoName.setText("Now watching: " + selectedVideo); // Use setText method to update the text
            }
        });
    }
}